/**
 * 
 */
package com.ermax.aircond.util.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ermax.aircond.manage.domain.RealTimeControl;
import com.ermax.aircond.util.service.MachineCommandService;

/**
 * @author Eric
 * 
 */
@Service("machineCommandService")
public class MachineCommandService {

	private static String COMMAND_PREFIX = "1";
	private static String SCHEDULE_PREFIX = "2";
	private static String EVENT_PREFIX = "3";

	private static String ON_OFF_TAG = "A";
	private static String ON_OFF_OPTION_ON = "1";
	private static String ON_OFF_OPTION_OFF = "0";

	private static String FAN_SPEED_TAG = "B";

	private static String ROOM_TEMPERATURE_TAG = "C";

	private static String DEFAULT_TEMPERATURE_TAG = "D";

	private static String MINIMUM_TEMPERATURE_TAG = "X";

	private static String KEY_LOCK_TAG = "K";
	// private static String KEY_LOCK_OPTION_NO_LOCK = "0";
	// private static String KEY_LOCK_OPTION_lOCK_FAN_SPEED_ONLY = "1";

	private static String VALVE_ON_OFF_TAG = "F";
	
	private static String VALVE_LEVEL_TAG = "V";

	private static String ERROR_MESSAGE_TAG = "E";

	public String getTypeString(String type) {
		if (null == type) {
			return null;
		} else {
			return "T" + type;
		}
	}

	// on = 1
	// off = 0

	public String getOnOffString(String onOff) {
		if (null == onOff) {
			return null;
		} else {
			return "A" + onOff;
		}
	}

	public String getOnOffString(boolean onOff) {
		if (onOff) {
			return ON_OFF_TAG + ON_OFF_OPTION_ON;
		} else {
			return ON_OFF_TAG + ON_OFF_OPTION_OFF;
		}
	}

	// 0=AUTO,1=HIGH,2=MID,3=LOW

	public String getFanSpeedString(String fanSpeed) {
		if (null == fanSpeed) {
			return null;
		} else {
			return FAN_SPEED_TAG + fanSpeed;
		}
	}

	public String getRoomTempString(int roomTemp) {
		// TODO Auto-generated method stub
		return ROOM_TEMPERATURE_TAG + roomTemp;
	}

	public String getGateTempString(int gateTemp) {
		// TODO Auto-generated method stub
		return "G" + gateTemp;
	}

	// 16~32

	public String getDefaultTempString(String defaultTemp) {

		return DEFAULT_TEMPERATURE_TAG + defaultTemp;
	}

	public String getValveOnOffString(int valueOnOff) {
		// TODO Auto-generated method stub
		return VALVE_ON_OFF_TAG + valueOnOff;
	}

	public String getValveLevelString(String level) {
		// TODO Auto-generated method stub
		return VALVE_LEVEL_TAG + level;
	}

	public String getMinimumTempString(String minTemp) {
		// TODO Auto-generated method stub
		return MINIMUM_TEMPERATURE_TAG + minTemp;
	}

	// 1or2
	public String getKeyLockString(String lock) {
		// TODO Auto-generated method stub
		return KEY_LOCK_TAG + lock;
	}

	public String getStatusString(int status) {
		// TODO Auto-generated method stub
		return "O" + status;
	}

	public String getFilterString(int filter) {
		// TODO Auto-generated method stub
		return "U" + filter;
	}

	public String getErrorString(String error) {
		// TODO Auto-generated method stub
		return "E" + error;
	}

	public int getGateNo(int nid) {
		return nid / 100;
	}
	
	public int getDeviceNo(int nid){
		return nid % 100;
	}

	public int getNid(int gateNo, int deviceNo) {
		return (gateNo * 100) + deviceNo;
	}

	public short getShortNid(int gateNo, int deviceNo) {
		Integer intNid = new Integer(this.getNid(gateNo, deviceNo));
		return intNid.shortValue();
	}

	public String getCommandString(String command) {
		return COMMAND_PREFIX + command;
	}

	public List<String> extractMachineData(String data) {
		List<String> results = new ArrayList<String>();
		if (data != null) {
			char[] chars = data.toCharArray();
			StringBuilder sb = null;
			for (char c : chars) {
				if (Character.isLetter(c)) {
					if (sb == null) {
						sb = new StringBuilder();
						sb.append(c);
					} else {
						results.add(sb.toString());
						sb = new StringBuilder();
						sb.append(c);
					}
				} else if (Character.isDigit(c)) {
					if (sb != null) {
						sb.append(c);
					}
				}
			}
			if (sb != null) {
				results.add(sb.toString());
			}
		}
		return results;
	}

	public RealTimeControl translateCommand(RealTimeControl realTimeControl, String command) {
		String content = command.substring(1);

		// 啟停 A A0停 A1啟
		if (command.startsWith(ON_OFF_TAG)) {
			if ("1".equals(content)) {
				realTimeControl.setStart(true);
			} else {
				realTimeControl.setStart(false);
			}
		}

		// 風速 B B0自動 B1高 B2中 B3低
		if (command.startsWith(FAN_SPEED_TAG)) {
			realTimeControl.setFanSpeed(content);
		}

		// 室內溫度
		if (command.startsWith(ROOM_TEMPERATURE_TAG)) {
			realTimeControl.setRoomTemperature(content);
		}

		// 設定溫度 D D16~D32
		if (command.startsWith(DEFAULT_TEMPERATURE_TAG)) {
			realTimeControl.setTemperature(content);
		}

		// 最低溫度 X X0,X16~X32
		if (command.startsWith(MINIMUM_TEMPERATURE_TAG)) {
			realTimeControl.setMinimumTemperature(content);
		}

		// 設定溫度 K K0-K1
		if (command.startsWith(KEY_LOCK_TAG)) {
			realTimeControl.setKeyLock(content);
		}

		// 設定溫度 E E3
		if (command.startsWith(ERROR_MESSAGE_TAG)) {
			realTimeControl.setErrorMessage(content);
		}

		// 風門開度 V V0~V100
		/*
		 * if(command.startsWith(VALVE_LEVEL_TAG)){
		 * realTimeControl.setWindValue(content); }
		 */
		return realTimeControl;
	}

	public static void main(String[] args) {
		// String data = "A1B2C24D11E54K13Z21";
		/*
		String data = "123123A43";
		MachineCommandService impl = new MachineCommandService();
		List<String> list = impl.extractMachineData(data);
		for (String str : list) {
			System.out.println(str);
		}
		*/
		MachineCommandService service = new MachineCommandService();
		int nid = 201;
		int gateNo = service.getGateNo(nid);
		int deviceNo = service.getDeviceNo(nid);
		System.out.println(gateNo);
		System.out.println(deviceNo);
		
	}
	
	public String getOnOffValue(List<String> list){
		Iterator<String> iter = list.iterator();
		while(iter.hasNext()){
			String str = iter.next();
			if(str != null && str.length() > 1){
				if(str.startsWith(ON_OFF_TAG)){
					
					return str.substring(1);
				}
			}
		}
		return null;
	}
	
	public String getFanSpeedValue(List<String> list){
		Iterator<String> iter = list.iterator();
		while(iter.hasNext()){
			String str = iter.next();
			if(str != null && str.length() > 1){
				if(str.startsWith(FAN_SPEED_TAG)){					
					return str.substring(1);
				}
			}
		}
		return null;
	}	
	
	public String getRoomTemperatureValue(List<String> list){
		Iterator<String> iter = list.iterator();
		while(iter.hasNext()){
			String str = iter.next();
			if(str != null && str.length() > 1){
				if(str.startsWith(ROOM_TEMPERATURE_TAG)){					
					return str.substring(1);
				}
			}
		}
		return null;
	}
	
	public String getDefaultTemperatureValue(List<String> list){
		Iterator<String> iter = list.iterator();
		while(iter.hasNext()){
			String str = iter.next();
			if(str != null && str.length() > 1){
				if(str.startsWith(DEFAULT_TEMPERATURE_TAG)){					
					return str.substring(1);
				}
			}
		}
		return null;
	}

	public String getMinimumTemperatureValue(List<String> list){
		Iterator<String> iter = list.iterator();
		while(iter.hasNext()){
			String str = iter.next();
			if(str != null && str.length() > 1){
				if(str.startsWith(MINIMUM_TEMPERATURE_TAG)){					
					return str.substring(1);
				}
			}
		}
		return null;
	}
	
	public String getKeyLockValue(List<String> list){
		Iterator<String> iter = list.iterator();
		while(iter.hasNext()){
			String str = iter.next();
			if(str != null && str.length() > 1){
				if(str.startsWith(KEY_LOCK_TAG)){					
					return str.substring(1);
				}
			}
		}
		return null;
	}
	
	public String getErrorMessageValue(List<String> list){
		Iterator<String> iter = list.iterator();
		while(iter.hasNext()){
			String str = iter.next();
			if(str != null && str.length() > 1){
				if(str.startsWith(ERROR_MESSAGE_TAG)){					
					return str.substring(1);
				}
			}
		}
		return null;
	}

}
